package com.zmh.sliderbar.myview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import androidx.annotation.Nullable;

public class MyText3 extends androidx.appcompat.widget.AppCompatTextView {

    private float mMaxPage=10;//最大页数
    private float moveableMinX=0;//可移动的最小X像素位置
    private float moveableMaxX=1437;//可移动的最大X像素位置
    private float currentX=0;//静态的当前位置
    private float currentMoveX=0;//动态的当前位置
    private float mPrePagePix=(moveableMaxX-moveableMinX)/mMaxPage;//每页对应的像素刻度
    private int currentPage =1;
    private float dx=0.0f;
    private int pageHis=1;

    private OnPageChangeListener mListener;
    public MyText3(Context context) {
        this(context,null);
    }

    public MyText3(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyText3(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    float downX;
    float moveX;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.d("zzz","操作:按下");
                downX = event.getRawX();//获取按下的点位距离左边屏幕边框的像素
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d("zzz","操作:滑动");
                moveX = event.getRawX();//获取移动后的点位距离左边屏幕边框的像素
                dx=moveX-downX;
                if((currentMoveX+ dx)<moveableMinX || (currentMoveX + dx)>moveableMaxX || (currentMoveX + dx)>mMaxPage*mPrePagePix){
                    Log.d("zzz","超出范围");
                    return true;//超出范围退出
                }
                int currentPage_tmp= (int) ((currentMoveX+dx)/mPrePagePix)+1; //临时滑动的页码
                if(currentPage_tmp>mMaxPage)
                {
                    currentPage_tmp=(int)mMaxPage;
                }
                if(currentPage_tmp==currentPage) //如果页码范围无变化
                {
                    this.setX(currentMoveX+dx);
                    Log.d("zzz","currentPage_tmp："+currentPage_tmp);
                    Log.d("zzz","currentPage："+currentPage);
                    Log.d("zzz","无页面更换滑动："+currentMoveX);
                    Log.d("zzz","无页面更换滑动："+dx);
                }else{
                    this.setX(currentMoveX+dx);
                    currentPage=currentPage_tmp;
                    this.setText(currentPage+"");
                    Log.d("zzz","有页面更换滑动："+currentMoveX);
                }
                return true;
            case MotionEvent.ACTION_UP:
                if(currentPage==mMaxPage)
                {
                    currentX=(currentPage)*mPrePagePix;
                }else{
                    currentX=(currentPage-1)*mPrePagePix;
                }
                currentMoveX=currentX;

                this.setX(currentX);
                Log.d("zzz","操作:抬起");
                this.setText(currentPage+"");
                if(mListener!=null && currentPage!=pageHis)
                {
                    mListener.onChange(currentPage);
                    pageHis=currentPage;
                }
                break;
        }
        return true;
    }

    public void initView(float max,float min,int maxPage){
        moveableMinX=min;
        moveableMaxX=max;
        mMaxPage=maxPage;
        mPrePagePix= (int) ((moveableMaxX-moveableMinX)/mMaxPage);
    }

    public interface OnPageChangeListener{
        void onChange(int page);
    }

    public void setOnPageChangeListener(OnPageChangeListener listener){
        mListener=listener;
    }
    public void setMax(int max){
        mMaxPage=max;
        mPrePagePix= (int) ((moveableMaxX-moveableMinX)/mMaxPage);
    }

    public void setProgress(int progress){
        if(progress<1 || progress>mMaxPage)
        {
            return;
        }
        currentPage=progress;
        if(currentPage==mMaxPage)
        {
            currentX=(currentPage)*mPrePagePix;
        }else{
            currentX=(currentPage-1)*mPrePagePix;
        }
        this.setX(currentX);
        this.setText(currentPage+"");

    }
}
